<?php
function template_admincp_url(){
    $CI =& get_instance();
    return $CI->config->base_url().'public/templates/admincp/';
}

function admincp_url(){
    $CI =& get_instance();
    return $CI->config->base_url().'admincp/';
}

function template_comingsoon_url(){
    $CI =& get_instance();
    return $CI->config->base_url().'public/comingsoon/';
}

function comingsoon_url(){
    $CI =& get_instance();
    return $CI->config->base_url().'comingsoon/';
}

function template_url(){
    $CI =& get_instance();
    return $CI->config->base_url().'public/templates/home/';
}

function pre($arr) {
    echo "<div style=\"border: 1px solid #000000; margin: 10px; font-size: 11px; color: blue; width: auto; clear: both; float: left; display:block; background-color:#F5F5DC\"><pre>";
    print_r($arr);
    echo "</pre></div>";
}

function change_alias ( $alias )
{
    $alias = nv_EncString( $alias );
   
    //thêm trường hợp các kí tự đặc biệt
    $alias = preg_replace( "/(!|\"|#|$|%|'|̣)/", '', $alias );
    $alias = preg_replace( "/(̀|́|̉|$|>)/", '', $alias );
    $alias = preg_replace( "'<[\/\!]*?[^<>]*?>'si", "", $alias );
   
    $alias = str_replace( "----", " ", $alias );
    $alias = str_replace( "---", " ", $alias );
    $alias = str_replace( "--", " ", $alias );
   
    $alias = preg_replace( '/(\W+)/i', '-', $alias );
    $alias = str_replace( array(
        '-8220-', '-8221-', '-7776-'
    ), '-', $alias );
    $alias = preg_replace( '/[^a-zA-Z0-9\-]+/e', '', $alias );
    $alias = str_replace( array(
        'dAg', 'DAg', 'uA', 'iA', 'yA', 'dA', '--', '-8230'
    ), array(
        'dong', 'Dong', 'uon', 'ien', 'yen', 'don', '-', ''
    ), $alias );
    $alias = preg_replace( '/(\-)$/', '', $alias );
    $alias = preg_replace( '/^(\-)/', '', $alias );
    return $alias;
}
 
/**
* EncString()
*
* @param mixed $text
* @return
*/
function nv_EncString ( $text )
{
    $text = html_entity_decode( $text );
    //thay thế chữ thuong
    $text = preg_replace( "/(å|ä|ā|à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|ä|ą)/u", 'a', $text );
    $text = preg_replace( "/(ß|ḃ)/", "b", $text );
    $text = preg_replace( "/(ç|ć|č|ĉ|ċ|¢|©)/", 'c', $text );
    $text = preg_replace( "/(đ|ď|ḋ|đ)/", 'd', $text );
    $text = preg_replace( "/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|ę|ë|ě|ė)/", 'e', $text );
    $text = preg_replace( "/(ḟ|ƒ)/", "f", $text );
    $text = str_replace( "ķ", "k", $text );
    $text = preg_replace( "/(ħ|ĥ)/", "h", $text );
    $text = preg_replace( "/(ì|í|î|ị|ỉ|ĩ|ï|î|ī|¡|į)/", 'i', $text );
    $text = str_replace( "ĵ", "j", $text );
    $text = str_replace( "ṁ", "m", $text );
   
    $text = preg_replace( "/(ö|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ö|ø|ō)/", 'o', $text );
    $text = str_replace( "ṗ", "p", $text );
    $text = preg_replace( "/(ġ|ģ|ğ|ĝ)/", "g", $text );
    $text = preg_replace( "/(ü|ù|ú|ū|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ü|ų|ů)/", 'u', $text );
    $text = preg_replace( "/(ỳ|ý|ỵ|ỷ|ỹ|ÿ)/", 'y', $text );
    $text = preg_replace( "/(ń|ñ|ň|ņ)/", 'n', $text );
    $text = preg_replace( "/(ŝ|š|ś|ṡ|ș|ş|³)/", 's', $text );
    $text = preg_replace( "/(ř|ŗ|ŕ)/", "r", $text );
    $text = preg_replace( "/(ṫ|ť|ț|ŧ|ţ)/", 't', $text );
   
    $text = preg_replace( "/(ź|ż|ž)/", 'z', $text );
    $text = preg_replace( "/(ł|ĺ|ļ|ľ)/", "l", $text );
   
    $text = preg_replace( "/(ẃ|ẅ)/", "w", $text );
   
    $text = str_replace( "æ", "ae", $text );
    $text = str_replace( "þ", "th", $text );
    $text = str_replace( "ð", "dh", $text );
    $text = str_replace( "£", "pound", $text );
    $text = str_replace( "¥", "yen", $text );
   
    $text = str_replace( "ª", "2", $text );
    $text = str_replace( "º", "0", $text );
    $text = str_replace( "¿", "?", $text );
   
    $text = str_replace( "µ", "mu", $text );
    $text = str_replace( "®", "r", $text );
   
    //thay thế chữ hoa
    $text = preg_replace( "/(Ä|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|Ą|Å|Ā)/", 'A', $text );
    $text = preg_replace( "/(Ḃ|B)/", 'B', $text );
    $text = preg_replace( "/(Ç|Ć|Ċ|Ĉ|Č)/", 'C', $text );
    $text = preg_replace( "/(Đ|Ď|Ḋ)/", 'D', $text );
    $text = preg_replace( "/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|Ę|Ë|Ě|Ė|Ē)/", 'E', $text );
    $text = preg_replace( "/(Ḟ|Ƒ)/", "F", $text );
    $text = preg_replace( "/(Ì|Í|Ị|Ỉ|Ĩ|Ï|Į)/", 'I', $text );
    $text = preg_replace( "/(Ĵ|J)/", "J", $text );
   
    $text = preg_replace( "/(Ö|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|Ø)/", 'O', $text );
    $text = preg_replace( "/(Ü|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|Ū|Ų|Ů)/", 'U', $text );
    $text = preg_replace( "/(Ỳ|Ý|Ỵ|Ỷ|Ỹ|Ÿ)/", 'Y', $text );
    $text = str_replace( "Ł", "L", $text );
    $text = str_replace( "Þ", "Th", $text );
    $text = str_replace( "Ṁ", "M", $text );
   
    $text = preg_replace( "/(Ń|Ñ|Ň|Ņ)/", "N", $text );
    $text = preg_replace( "/(Ś|Š|Ŝ|Ṡ|Ș|Ş)/", "S", $text );
    $text = str_replace( "Æ", "AE", $text );
    $text = preg_replace( "/(Ź|Ż|Ž)/", 'Z', $text );
   
    $text = preg_replace( "/(Ř|R|Ŗ)/", 'R', $text );
    $text = preg_replace( "/(Ț|Ţ|T|Ť)/", 'T', $text );
    $text = preg_replace( "/(Ķ|K)/", 'K', $text );
    $text = preg_replace( "/(Ĺ|Ł|Ļ|Ľ)/", 'L', $text );
   
    $text = preg_replace( "/(Ħ|Ĥ)/", 'H', $text );
    $text = preg_replace( "/(Ṗ|P)/", 'P', $text );
    $text = preg_replace( "/(Ẁ|Ŵ|Ẃ|Ẅ)/", 'W', $text );
    $text = preg_replace( "/(Ģ|G|Ğ|Ĝ|Ġ)/", 'G', $text );
    $text = preg_replace( "/(Ŧ|Ṫ)/", 'T', $text );
   
    return $text;
}


function curPageURL() {
    $pageURL = 'http://';
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

if (!function_exists('trans')) {

    function trans($word, $return = FALSE) {
        $CI = & get_instance();
        $CI->load->model('admincp/mtranslate', 'translate');
        $trans = $CI->translate->get($word);
        if ($return == FALSE) {
            return $trans;
        } else {
            return $trans;
        }
    }

}

if (!function_exists('isValidMd5')) {
    function isValidMd5($md5 = '')
    {
        return   preg_match('/^[a-f0-9]{32}$/', $md5);
    }
    
}

if (!function_exists('setting')) {

    function setting($col) {
        $CI = & get_instance();
        $CI->load->model('Msetting', 'setting');
        $setting = $CI->setting->get($col);
        if ($setting == $col) {
            echo $col.' in setting empty data';
        } else {
            return $setting[$col];
        }
    }

}

if(!function_exists('convert_utf8')) {
    function convert_utf8($text){
        $text = str_replace('&Agrave;','À',$text);
        $text = str_replace('&Aacute;','Á',$text);
        $text = str_replace('&Acirc;','Â',$text);
        $text = str_replace('&Atilde;','Ã',$text);
        $text = str_replace('&Egrave;','È',$text);
        $text = str_replace('&Eacute;','É',$text);
        $text = str_replace('&Ecirc;','Ê',$text);
        $text = str_replace('&Igrave;','Ì',$text);
        $text = str_replace('&Iacute;','Í',$text);
        $text = str_replace('&ETH;','Đ',$text);
        $text = str_replace('&Ograve;','Ò',$text);
        $text = str_replace('&Oacute;','Ó',$text);
        $text = str_replace('&Ocirc;','Ô',$text);
        $text = str_replace('&Otilde;','Õ',$text);
        $text = str_replace('&Ugrave;','Ù',$text);
        $text = str_replace('&Uacute;','Ú',$text);
        $text = str_replace('&Yacute;','Ý',$text);
        $text = str_replace('&agrave;','à',$text);
        $text = str_replace('&aacute;','á',$text);
        $text = str_replace('&acirc;','â',$text);
        $text = str_replace('&atilde;','ã',$text);
        $text = str_replace('&egrave;','è',$text);
        $text = str_replace('&eacute;','é',$text);
        $text = str_replace('&ecirc;','ẽ',$text);
        $text = str_replace('&igrave;','ì',$text);
        $text = str_replace('&iacute;','í',$text);
        $text = str_replace('&ograve;','ò',$text);
        $text = str_replace('&oacute;','ó',$text);
        $text = str_replace('&ocirc;','ô',$text);
        $text = str_replace('&otilde;','õ',$text);
        $text = str_replace('&ugrave;','ù',$text);
        $text = str_replace('&uacute;','ú',$text);
        $text = str_replace('&yacute;','ý',$text);
        return $text;
    }   
}

if (!function_exists('create_directory')) {

    function create_directory($path,$name){     
        $path_dir = $path.$name;
        if (!file_exists($path_dir)) {
            mkdir('./'.$path_dir, 0777);
        }else{
            return FALSE;   
        }
    }

}

if (!function_exists('write_log')) {

    function write_log($path = "",$user ="",$data = ""){
        if($user == ""){
            $user = "";
        }
        $f = fopen($path,'a');
        fwrite($f,date("Y-m-d H:i:s").chr(9).$user.chr(9).$data."\r\n");
        fclose($f); 
    }

}

function exit_status($str){
    echo json_encode(array('status'=>$str));
    exit;
}

function get_extension($file_name){
    $ext = explode('.', $file_name);
    $ext = array_pop($ext);
    return strtolower($ext);
}

function resizeImage($width, $height, $tmp_name, $path, $type){
	/* Get original image x y*/
	list($w, $h) = getimagesize($path.$tmp_name);
	/* calculate new image size with ratio */
	$ratio = max($width/$w, $height/$h);
	$h = ceil($height / $ratio);
	$x = ($w - $width / $ratio) / 2;
	$w = ceil($width / $ratio);
	/* new file name */
	// $path = 'uploads/'.$width.'x'.$height.'_'.$name;
	/* read binary data from image file */
	$imgString = file_get_contents($path.$tmp_name);
	/* create image from string */
	$image = imagecreatefromstring($imgString);
	$tmp = imagecreatetruecolor($width, $height);
	imagecopyresampled($tmp, $image,
  	0, 0,
  	$x, 0,
  	$width, $height,
  	$w, $h);
	/* Save image */
	switch ($type) {
		case 'image/jpeg':
			imagejpeg($tmp, $path, 100);
			break;
		case 'image/png':
			imagepng($tmp, $path, 0);
			break;
		case 'image/gif':
			imagegif($tmp, $path);
			break;
		default:
			exit;
			break;
	}
	// return $path;
	/* cleanup memory */
	imagedestroy($image);
	imagedestroy($tmp);
}

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        
        //close the zip -- done!
        $zip->close();
        
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}